package evan.curtis.beersnob;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;

public class MainActivity extends Activity {

	private ActionBar actionBar;
	private Activity mActivity;
	private SearchFragment mSearchFragment;
	private TopRatedFragment mTopRatedFragment;
	private FavoritesFragment mFavoritesFragment;

	/**********************************************************************************************/

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		// -------------------------------------------------------------------------------/
		// Search Fragment
		String label = getResources().getString(R.string.search);
		Tab tab = actionBar.newTab();
		tab.setText(label);
		TabListener<SearchFragment> t0 = new TabListener<SearchFragment>(this,
				label, SearchFragment.class);
		tab.setTabListener(t0);
		actionBar.addTab(tab);

		tab.select();
		mSearchFragment = (SearchFragment) t0.mFragment;

		// --------------------------------------------------------------------------------/
		// Top Rated Fragment
		String label1 = getResources().getString(R.string.top_rated);
		tab = actionBar.newTab();
		tab.setText(label1);
		TabListener<TopRatedFragment> tl = new TabListener<TopRatedFragment>(
				this, label1, TopRatedFragment.class);
		tab.setTabListener(tl);
		actionBar.addTab(tab);

		tab.select();
		mTopRatedFragment = (TopRatedFragment) tl.mFragment;

		// ---------------------------------------------------------------------------------/
		// Favorites Fragment
		String label2 = getResources().getString(R.string.my_favorites);
		tab = actionBar.newTab();
		tab.setText(label2);
		TabListener<FavoritesFragment> t2 = new TabListener<FavoritesFragment>(
				this, label2, FavoritesFragment.class);
		tab.setTabListener(t2);
		actionBar.addTab(tab);

		tab.select();
		mFavoritesFragment = (FavoritesFragment) t2.mFragment;

		// ----------------------------------------------------------------------------------/

		// return action bar to first tab
		tab = actionBar.getTabAt(0);
		tab.select();

	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.menu, menu);
		return true;
	}

	private class TabListener<T extends Fragment> implements
			ActionBar.TabListener {
		private Fragment mFragment;
		private final Activity mActivity;
		private final String mTag;
		private final Class<T> mClass;

		/**
		 * Constructor used each time a new tab is created.
		 * 
		 * @param activity
		 *            The host Activity, used to instantiate the fragment
		 * @param tag
		 *            The identifier tag for the fragment
		 * @param clz
		 *            The fragment's Class, used to instantiate the fragment
		 */
		public TabListener(Activity activity, String tag, Class<T> clz) {
			mActivity = activity;
			mTag = tag;
			mClass = clz;
		}

		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			// Check if the fragment is already initialized
			if (mFragment == null) {
				// If not, instantiate and add it to the activity
				mFragment = Fragment.instantiate(mActivity, mClass.getName());
				ft.add(android.R.id.content, mFragment, mTag);
			} else {
				// If it exists, simply attach it in order to show it
				ft.attach(mFragment);
			}
		}

		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			if (mFragment != null) {
				// Detach the fragment, because another one is being attached
				ft.detach(mFragment);
			}
		}

		public void onTabReselected(Tab tab, FragmentTransaction ft) {
			// User selected the already selected tab. Usually do nothing.
		}

	}

	/**********************************************************************************************/

}
